package com.sun.lotter.local.record;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.sun.base.UserActivity;
import com.sun.bata.LotterData;
import com.sun.bata.SQLlite;
import com.sun.lotter.R;

public class LotterLocalRecordActivity extends UserActivity
{

	SimpleAdapter listAdapter;

	@Override
	public void init()
	{
		noTitle = true;
		mainLayout.addView(inflater.inflate(R.layout.localrecord, null));
		list = new ArrayList<Map<String, String>>();
		mainLayout.findViewById(R.id.button1).setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				getData();
				listAdapter.notifyDataSetChanged();
			}
		});
		ListView listView = (ListView) mainLayout.findViewById(R.id.listView);
		listView.setCacheColorHint(0);
		getData();
		listAdapter = new SimpleAdapter(this, list, R.layout.localrecord_item, new String[]
		{ "time", "lotterName", "lotterplay", "lotterNumbers" }, new int[]
		{ R.id.textView_time, R.id.textView_lottertype, R.id.textView_playtype, R.id.textView_numbers })
		{

			@Override
			public View getView(final int position, View convertView, ViewGroup parent)
			{
				View view = super.getView(position, convertView, parent);
				view.findViewById(R.id.button1).setOnClickListener(new OnClickListener()
				{

					@Override
					public void onClick(View v)
					{
						SQLlite dbLlite = new SQLlite(LotterLocalRecordActivity.this);
						dbLlite.deleteRaw(LotterData.getLotEName(lotterId), position);
						list.remove(position);
						notifyDataSetChanged();
					}
				});
				return view;
			}
		};
		listView.setAdapter(listAdapter);
	}

	String[] form = {};
	ArrayList<Map<String, String>> list;

	private ArrayList<Map<String, String>> getData()
	{
		list.removeAll(list);
		SQLlite dbLlite = new SQLlite(this);
		Cursor c = dbLlite.rawQuery(LotterData.getLotEName(lotterId));
		c.moveToFirst();
		while (c.moveToNext())
		{
			HashMap<String, String> values = new HashMap<String, String>();
			values.put("time", c.getString(c.getColumnIndex("time")));
			values.put("lotterName", c.getString(c.getColumnIndex("lotterName")));
			values.put("lotterplay", c.getString(c.getColumnIndex("lotterplay")));
			values.put("lotterNumbers", c.getString(c.getColumnIndex("lotterNumbers")));
			list.add(values);
		}
		dbLlite.close();
		return list;
	}
}
